package cn.linran._01_synchronization;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 同步
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.0实例化消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("sync_group_name");
        //1.1设置NameServer的地址
        consumer.setNamesrvAddr("localhost:9876");
        //1.2订阅一个或者多个Topic，以及Tag来过滤需要消费的信息
        consumer.subscribe("Topic_sync","*");
        //1.3注册回调实现类来处理从broker拉取回来的信息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                //1.4打印接收的消息
                System.out.printf("%s Receiver New Messages:%s %n",Thread.currentThread().getName(),list);
                //1.5标记该消费已经被成功消费
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //1.6启动消费实例
        consumer.start();
        System.out.printf("Consumer Started.%n");

    }
}
